package com.songcha.library_base.compose

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_base.interfaces.ILoadMoreData
import com.songcha.library_base.interfaces.IRefreshData
import com.songcha.library_base.interfaces.IResult
import com.songcha.library_base.mvvm.base.BaseRefreshLoadMoreViewModel
import com.songcha.library_base.mvvm.base.BaseRefreshViewModel
import com.songcha.library_base.mvvm.base.BaseRepository


abstract class BaseComposeRefreshLoadMoreViewModel<R: BaseRepository,RLST>(app: Application) : BaseRefreshLoadMoreViewModel<R, RLST>(app),
    IRefreshData, ILoadMoreData {
    //val rlmlst = MutableLiveData<MutableList<Any>>()

    /***
     * handle load,refresh,loadmore state when getdata success
     */
    override fun handleDataListSuccess(newList:MutableList<RLST>){
        if(isRefreshing){
            if(newList.size == 0){
                onRefreshNoData()
            }else{
                rlst.value = newList
                onRefreshSuccess()
            }
        }else if(isLoadingMore){
            if(newList.size == 0){
                onLoadMoreNoData()
            }else{
                var start = 0
                var size = newList.size
                if(rlst.value == null || rlst.value!!.size == 0){
                    rlst.value = newList
                }else{
                    //compose list must be deep copy
                    val tmp = rlst.value!!.toMutableList()
                    start = tmp!!.size
                    size = newList.size
                    tmp.addAll(newList)
                    rlst.value = tmp
                }
                onLoadMoreSuccess(start,size)
            }

        }else{
            if(newList.size == 0){
                onLoadNoData()
            }else{
                rlst.value = newList
                onLoadSuccess()
            }
        }

    }


}